from configs import config_manage
from common.sqlsever_helper import SqlServer
from common import logger
import time
import hmac
import hashlib
import base64
import urllib.parse

# 日志
log = logger.Logger(name='base_config')  # 公共调用，防止多次实例初始化


# 数据库实例化
DB_ZB = SqlServer("ZB")
DB_MD = SqlServer("MD")



# 配置文件实例化
loc = config_manage.get_yaml_page_loc(r'configs.yaml')

# 接口常用URL,BASE_HEADER
BASE_URL = loc['URL']
BASE_URL_ZB = loc['ZB_URL']
BASE_HEADER = {
        "Content-Type": "application/json",
        "Accept-Charset": "utf-8"
    }


# 钉钉机器人url
# DINGDING_API = "https://oapi.dingtalk.com/robot/send?access_token=385528a189f063859b85c3bd7509b3ca668c7ae6b8ceccc0b0ae22522b4a80c9"
def get_dingding_api():
    timestamp = round(time.time() * 1000)  # 时间戳
    secret = 'SECa0ab7f3ba9742c0b6c36d8808bf7b16e92706db52e1d09260193b6d466f149a8'  # 加签秘钥
    secret_enc = secret.encode('utf-8')
    string_to_sign = '{}\n{}'.format(timestamp, secret)
    string_to_sign_enc = string_to_sign.encode('utf-8')
    hmac_code = hmac.new(secret_enc, string_to_sign_enc, digestmod=hashlib.sha256).digest()
    sign = urllib.parse.quote_plus(base64.b64encode(hmac_code))  # 最终签名
    print(timestamp)
    print(sign)

    webhook_url = "https://oapi.dingtalk.com/robot/send?access_token=1554a3dd1e748cbf748e8e0ea9d3cb61c2ee008d32167313389ff14cccda34a2"  # 无加密的url

    DINGDING_API = webhook_url + '&timestamp={}&sign={}'.format(timestamp, sign)  # 最终url，url+时间戳+签名
    print(DINGDING_API)
    return DINGDING_API

# 游乐宝/大客户-后台/收银台
YLB_BG = "YLB-BackGround"
YLB_CT = "YLB-CashTerminal"
DKH_BG = "DKH-BackGround"
DKH_CT = "DKH-CashTerminal"
DKH_ZB = "DKH-ZB"


if __name__ == '__main__':
    # get_mac_address()
    get_dingding_api()
    pass
